package org.edu.modules.jxgz.controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jxgz.api.CommonApi;
import org.edu.modules.jxgz.entity.VeJwBjgg;
import org.edu.modules.jxgz.entity.VeJwText;
import org.edu.modules.jxgz.service.IVeJwBjggService;
import org.edu.modules.jxgz.util.VeJwTextUtils;
import org.edu.modules.scheduleCommon.entity.VeBaseAppUser;
import org.edu.modules.scheduleCommon.entity.VeBaseBanji;
import org.edu.modules.scheduleCommon.entity.VeBaseStudent;
import org.edu.modules.scheduleCommon.service.VeBaseAppUserService;
import org.edu.modules.scheduleCommon.service.VeBaseBanjiService;
import org.edu.modules.scheduleCommon.service.VeBaseStudentService;
import org.edu.modules.scheduleCommon.service.VeBaseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 班级公告信息表
 * @Author: jeecg-boot
 * @Date: 2021-04-27
 * @Version: V1.0
 */
@Api(tags = "班级公告信息表")
@RestController
@RequestMapping("/jxgz/veJwBjgg")
@Slf4j
public class VeJwBjggController extends BaseController<VeJwBjgg, IVeJwBjggService> {
    @Autowired
    private IVeJwBjggService veJwBjggService;

    @Autowired
    private CommonApi commonApi;

    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    @Autowired
    private VeBaseBanjiService veBaseBanjiService;

    @Autowired
    private VeBaseStudentService veBaseStudentService;

    @Autowired
    private VeBaseTeacherService veBaseTeacherService;

    /**
     * 分页列表查询
     *
     * @param veJwBjgg
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "班级公告信息表-分页列表查询")
    @ApiOperation(value = "班级公告信息表-分页列表查询", notes = "班级公告信息表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VeJwBjgg veJwBjgg,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        QueryWrapper<VeJwBjgg> queryWrapper = QueryGenerator.initQueryWrapper(veJwBjgg, req.getParameterMap());
        Page<VeJwBjgg> page = new Page<VeJwBjgg>(pageNo, pageSize);
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        if (appUser.getUserType().contains("2")) {
            //学生，根据学号查班级
            VeBaseStudent baseStudent = veBaseStudentService.findByUserId(appUser.getUserId());
            if (baseStudent != null) {
                queryWrapper.eq("bjId", baseStudent.getBjId());
                queryWrapper.lt("addtime", LocalDateTime.now());
            }
        } else if (appUser.getUserType().contains("1")) {
            //如果用户类型是1，为教师账号
            List<Integer> bjIdsList = veBaseTeacherService.getBjIdsListBySysUserId(0,appUser.getUserId());
            queryWrapper.in("bjId", bjIdsList);
        }
        IPage<VeJwBjgg> pageList = veJwBjggService.page(page, queryWrapper);
        for (VeJwBjgg temp : pageList.getRecords()) {
            if (StringUtils.isNotBlank(String.valueOf(temp.getContenttid()))) {
                VeJwText veJwText = VeJwTextUtils.getVeJwTextById(Integer.valueOf(temp.getContenttid()));
                if (veJwText != null) {
                    temp.setContenttidText(veJwText.getText());
                }
            }
            if (temp.getVerifytid() != null) {
                VeJwText veJwText = VeJwTextUtils.getVeJwTextById(Integer.valueOf(temp.getVerifytid()));
                if (veJwText != null) {
                    temp.setVerifytidText(veJwText.getText());
                }
            }
        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param veJwBjgg
     * @return
     */
    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJwBjgg veJwBjgg) {
        String rznrtidText = veJwBjgg.getContenttidText();
        if (StringUtils.isBlank(rznrtidText)) {
            return Result.error("内容不可为空");
        }
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        veJwBjgg.setAddusername(appUser.getRealName());
        //根据班级id查班级
        VeBaseBanji veBaseBanji = veBaseBanjiService.findById(veJwBjgg.getBjid());
        if (veBaseBanji != null) {
            veJwBjgg.setBjmc(veBaseBanji.getXzbmc());
        }
        veJwBjgg.setContenttid(VeJwTextUtils.add(rznrtidText));
        veJwBjggService.save(veJwBjgg);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param veJwBjgg
     * @return
     */
    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwBjgg veJwBjgg) {
        String rznrtidText = veJwBjgg.getContenttidText();
        if (StringUtils.isBlank(rznrtidText)) {
            return Result.error("内容不可为空");
        }
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        veJwBjgg.setAddusername(appUser.getRealName());
        //根据班级id查班级
        VeBaseBanji veBaseBanji = veBaseBanjiService.findById(veJwBjgg.getBjid());
        if (veBaseBanji != null) {
            veJwBjgg.setBjmc(veBaseBanji.getXzbmc());
        }
        veJwBjgg.setContenttid(VeJwTextUtils.addOrUpdate(Integer.valueOf(veJwBjgg.getContenttid()), rznrtidText));
        veJwBjggService.updateById(veJwBjgg);
        veJwBjggService.initVerifyt(veJwBjgg);
        return Result.ok("编辑成功!");
    }

    /**
     * 评审
     *
     * @param veJwBjgg
     * @return
     */
    @AutoLog(value = "评审")
    @ApiOperation(value = "评审", notes = "评审")
    @PutMapping(value = "/audit")
    public Result<?> audit(@RequestBody VeJwBjgg veJwBjgg) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        veJwBjgg.setVerifyuser(appUser.getUserId());
        veJwBjgg.setVerifyusername(appUser.getRealName());
        if (veJwBjgg.getVerifytid() != null) {
            veJwBjgg.setVerifytid(VeJwTextUtils.add(veJwBjgg.getVerifytidText()));
        } else {
            veJwBjgg.setVerifytid(VeJwTextUtils.addOrUpdate(veJwBjgg.getVerifytid(), veJwBjgg.getVerifytidText()));
        }
        veJwBjgg.setVerifytime(new Date());
        veJwBjggService.updateById(veJwBjgg);
        return Result.ok("评审成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ve_jw_bjgg-通过id删除")
    @ApiOperation(value = "ve_jw_bjgg-通过id删除", notes = "ve_jw_bjgg-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        veJwBjggService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ve_jw_bjgg-批量删除")
    @ApiOperation(value = "ve_jw_bjgg-批量删除", notes = "ve_jw_bjgg-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.veJwBjggService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ve_jw_bjgg-通过id查询")
    @ApiOperation(value = "ve_jw_bjgg-通过id查询", notes = "ve_jw_bjgg-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwBjgg veJwBjgg = veJwBjggService.getById(id);
        if (veJwBjgg == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(veJwBjgg);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param veJwBjgg
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwBjgg veJwBjgg) {
        return super.exportXls(request, veJwBjgg, VeJwBjgg.class, "ve_jw_bjgg");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwBjgg.class);
    }

}
